﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace ThuChi
{
    public partial class NhomVaDoiTac : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int maQuyen;
            try
            {
                maQuyen = Utilities.GetMaQuyen((int)Session["MaTaiKhoan"]);

            }
            catch //(Exception ex)
            {
                maQuyen = 0;
            }

            if (maQuyen == 0)
            {
                Session["RefUrl"] = "~/NhomVaDoiTac.aspx";
                Response.Redirect("~/Login.aspx");
            }
        }

        protected void grvNhom_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Trang " + (grvNhom.PageIndex + 1) + " của " + grvNhom.PageCount;
            }
        }

        protected void grvNhom_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                lblNhomErrorMessage.Text = "Không thể xóa nhóm: " + e.Values[0].ToString();
                e.ExceptionHandled = true;
            }
            else
            {
                lblNhomErrorMessage.Text = "";
                Session["MaNhom"] = 0;
            }
        }

        protected void grvNhom_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["MaNhom"] = grvNhom.SelectedValue;
        }

        protected void cboNhomPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvNhom.PageSize = Convert.ToInt16(cboNhomPageSize.SelectedValue);
        }

        protected void grvDoiTac_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Trang " + (grvDoiTac.PageIndex + 1) + " của " + grvDoiTac.PageCount;
            }
        }

        protected void grvDoiTac_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                lblDoiTacErrorMessage.Text = "Không thể xóa đối tác " + e.Values[0].ToString();// + " \n" + e.Exception.Message.ToString();
                e.ExceptionHandled = true;
            }
            else
            {
                lblDoiTacErrorMessage.Text = "";
            }
        }

        protected void cboDoiTacPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvDoiTac.PageSize = Convert.ToInt16(cboDoiTacPageSize.SelectedValue);
        }

        protected void DetailsViewNhom_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                lblNhomErrorMessage.Text = "Vui lòng nhập tên nhóm!";
                DetailsViewNhom.Focus();
                e.ExceptionHandled = true;
            }
            else
            {
                grvNhom.DataBind();
                grvNhom.PageIndex = grvNhom.PageCount - 1;
                grvNhom.SelectedIndex = grvNhom.Rows.Count - 1;
                DetailsViewDoiTac.Focus();
                lblNhomErrorMessage.Text = "";
            }
        }

        protected void DetailsViewDoiTac_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (Session["MaNhom"] == null || (int)Session["MaNhom"] == 0)
            {
                lblDoiTacErrorMessage.Text = "Vui lòng chọn một nhóm!";
                grvNhom.Focus();
                e.ExceptionHandled = true;
            }
            else if (e.Exception != null)
            {
                lblDoiTacErrorMessage.Text = "Vui lòng nhập tên đối tác!";
                e.ExceptionHandled = true;
                DetailsViewDoiTac.Focus();
            }
            else
            {
                grvDoiTac.DataBind();
                grvDoiTac.PageIndex = grvDoiTac.PageCount - 1;
                lblDoiTacErrorMessage.Text = "";
                DetailsViewDoiTac.Focus();
            }
        }

        protected void LinqDataSourceNhom_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            cboNhomPageSize_SelectedIndexChanged(sender, e);
        }

        protected void LinqDataSourceDoiTac_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            cboDoiTacPageSize_SelectedIndexChanged(sender, e);
        }

        protected void grvNhom_PreRender(object sender, EventArgs e)
        {
            grvNhom.DataBind();
            int rc = grvNhom.Rows.Count - 1;
            if (grvNhom.SelectedIndex > rc)
            {
                grvNhom.SelectedIndex = 0;
            }
            grvNhom_SelectedIndexChanged(sender, e);
            //grvDoiTac.DataBind();
        }

        protected void grvDoiTac_PreRender(object sender, EventArgs e)
        {
            grvDoiTac.DataBind();
        }
    }
}